#include "MyString.h"
#include "string.h"

//重载 << 左移操作符
                //此时的成员函数就不行了 用友元函数来做
ostream& operator<<(ostream &out, MyString &s)
{
        out<<s.p;
        return out;
}

//重载 >> 右移操作符
                //此时的成员函数就不行了 用友元函数来做
istream& operator>>(istream &in, MyString &s)
{
        in>>s.p;
        return in;
}

MyString::MyString(int len)
{
        if(len == 0){
                this->len = 0;
                p = new char[(this->len+1)*sizeof(char)];
                memset(p, 0, this->len+1);
                strcpy(p, "");
        }else{
                this->len = len;
                this->p = new char[this->len+1];
                memset(this->p, 0, this->len);
        }

}

//有参构造函数
MyString::MyString(const char* p)
{
        if(p == NULL){
                this->len = 0;
                this->p = new char[(this->len+1)*sizeof(char)];
                memset(this->p, 0, this->len+1);
                strcpy(this->p, "");
        }else{
                this->len = strlen(p);
                this->p = new char[(this->len+1)*sizeof(char)];
                memset(this->p, 0, this->len+1);
                strcpy(this->p, p);
        }
}

//赋值(拷贝)构造函数
MyString::MyString(const MyString& s )
{
        this->len = s.len;
        this->p = new char[(this->len+1)*sizeof(char)];
        memset(this->p, 0, this->len+1);
        strcpy(this->p, s.p);
}

//析够函数
MyString::~MyString()
{
        if(p){
                delete [] p;
                len = 0;
        }
}

//重载 = 号操作符
//s4 = "ssss";
MyString& MyString::operator=(const char *p)
{
        //先把旧的内存释放掉
        if(this->p != NULL){
                delete [] this->p;
                this->len = 0;
                this->p = NULL;
        }
        //根据p分配内存
        if(p == NULL){
                this->len = 0;
                this->p = new char[this->len+1];
                strcpy(this->p, "");  
        }else{
                this->len = strlen(p);
                this->p = new char[this->len+1];
                strcpy(this->p, p);
        }

        return *this;
}
//s4 = s2
MyString& MyString::operator=(const MyString &s )
{
        //先把旧的内存释放掉
        if(this->p != NULL){
                delete [] this->p;
                this->len = 0;
                this->p = NULL;
        }
        //根据s分配内存
        this->len = s.len;
        this->p = new char[this->len+1];
        strcpy(this->p, s.p);

        return *this;       
}

//重载 [ ]号操作符
char& MyString::operator[](int index)
{
        return p[index];
}

//重载 == 双等号操作符
//s == "aa"
bool MyString::operator==(const char *p)
{
        if(p == NULL)
        {
                if(this->len == 0)
                {
                        return true;
                }else
                {
                        return false;
                }
        }else{
                if(this->len == (int)strlen(p))
                {
                        return !strcmp(this->p, p);
                }else{
                        return false;
                }
        }
}
//s1 == s2 
bool MyString::operator==(const MyString &s)
{
        if(len != s.len) return false;
        return !strcmp(p, s.p);
}

//重载 != 不等号操作符
//s != "cc"
bool MyString::operator!=(const char *p)
{
        return !(*this == p);
}
// s1 != s2
bool MyString::operator!=(const MyString &s)
{
        return !(*this == s);
}


//重载 > 大于号操作符
int MyString::operator>(const char *p)
{
        return strcmp(p, this->p);
}
int MyString::operator>(const MyString &s)
{
        return strcmp(s.p, this->p);
}
//重载 < 小于号操作符
int MyString::operator<(const char *p)
{
        return strcmp(this->p, p);
}
int MyString::operator<(const MyString &s)
{
        return strcmp(this->p, s.p);
}

